﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/9 9:45:33
 * ************************************************************/
namespace Mozlite.Web.Controls
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using System.Linq.Expressions;

    /// <summary>
    /// CheckBoxList的MVC扩展。
    /// </summary>
    public static class CheckBoxListExtension
    {
        /// <summary>
        /// 复选框扩展。
        /// </summary>
        /// <typeparam name="TModel">模型类型。</typeparam>
        /// <typeparam name="TProperty">属性类型。</typeparam>
        /// <param name="helper">HTML辅助方法。</param>
        /// <param name="expression">lambda表达式。</param>
        /// <param name="selectList">选择项。</param>
        /// <param name="htmlAttributes">HTML属性。</param>
        /// <returns>返回复选框MVC的字符串。</returns>
        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList = null, ButtonFormatter formatter = ButtonFormatter.Horizontal, IDictionary<string, object> htmlAttributes = null)
        {
            if (selectList == null || expression == null)
                return MvcHtmlString.Empty;
            string name = ExpressionHelper.GetExpressionText(expression);
            List<string> values = null;
            object obj = helper.ViewData.Eval(name);
            if (obj != null)
                values = obj.ToString().Split<string>();
            else
                values = new List<string>();
            StringBuilder sb = new StringBuilder();
            int index = 0;
            foreach (var item in selectList)
            {
                TagBuilder tag = new TagBuilder("input");
                tag.MergeAttributes<string, object>(htmlAttributes);
                tag.MergeAttribute("type", "checkbox", true);
                tag.MergeAttribute("name", name, true);
                tag.MergeAttribute("id", name + index, true);
                tag.MergeAttribute("value", item.Value, true);
                if (values.Contains(item.Value))
                    tag.MergeAttribute("checked", "checked", true);
                sb.AppendLine(tag.ToString(TagRenderMode.SelfClosing) + " ");
                TagBuilder label = new TagBuilder("label");
                label.MergeAttribute("for", name + index);
                label.InnerHtml = item.Text;
                sb.AppendLine(label.ToString());
                if (formatter == ButtonFormatter.Vertical)
                    sb.AppendLine("<br />");
                index++;
            }
            return new MvcHtmlString(sb.ToString());
        }

        /// <summary>
        /// 复选框扩展。
        /// </summary>
        /// <typeparam name="TModel">模型类型。</typeparam>
        /// <typeparam name="TProperty">属性类型。</typeparam>
        /// <param name="helper">HTML辅助方法。</param>
        /// <param name="expression">lambda表达式。</param>
        /// <param name="selectList">选择项。</param>
        /// <param name="htmlAttributes">HTML属性。</param>
        /// <returns>返回复选框MVC的字符串。</returns>
        public static MvcHtmlString CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList = null, ButtonFormatter formatter = ButtonFormatter.Horizontal, object htmlAttributes = null)
        {
            return helper.CheckBoxListFor<TModel, TProperty>(expression, selectList, formatter, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
    }
}